package com.scpii.universal.struts.produce;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.scpii.universal.action.BaseAction;
import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.AttributeDto;
import com.scpii.universal.dto.ContentDto;
import com.scpii.universal.dto.ContentImgDtoForPru;
import com.scpii.universal.hibernate.entity.AttributeTab;
import com.scpii.universal.hibernate.entity.ContentImgTab;
import com.scpii.universal.hibernate.entity.ContentTab;
import com.scpii.universal.service.AppContentService;
import com.scpii.universal.service.AppPageService;
import com.scpii.universal.service.CatelogService;
import com.scpii.universal.service.ContentImgService;
import com.scpii.universal.service.ContentService;

/**
 * universalWeb
 * @author liuhj
 * 2012-8-7下午02:37:15
 */
@Controller
@Scope("prototype")
public class ContentProcessAction extends BaseAction {

	private static final long serialVersionUID = -5449279399596308603L;
	private Log log = LogFactory.getLog(this.getClass());
	private String json;
	private List<AttributeTab> lstAttributes;
	
	@Resource
	private AppContentService appContentService;
	@Resource
	private ContentService contentService;
	@Resource
	private ContentImgService contentImgService;
	@Resource
	private CatelogService catelogService;
	@Resource
	private AppPageService appPageService;

	/**
	 * 根据分类ID查询内容列表
	 * @return
	 * @author liuhj
	 * 2012-8-7下午02:38:46
	 * @throws IOException 
	 */
	public String listContentInfo() throws IOException {
		int cateId = JSONObject.fromObject(json).getInt("intCateId");
		Integer pageNum = JSONObject.fromObject(json).getInt("pageNum");
		List<ContentTab> contents = appContentService.getContentIdsByCateId(
				cateId, pageNum == null ? 1 : pageNum,
				GenericConstants.PAGE_SIZE_FOR_PRODUCE);
		if (contents == null || contents.size() <= 0) {
			jsonOut(GenericConstants.OPERATION_NULL);
			log.info("分类：" + cateId + "没有内容记录");
			return null;
		}
		JSONObject outDatas = new JSONObject();
		if (pageNum == 1) {
			int totalCount = appContentService.getCountById(String
					.valueOf(cateId), getUserInfo().getAppId());
			outDatas.put("totalPageNo", totalCount
					/ GenericConstants.PAGE_SIZE_FOR_PRODUCE);
		}
		JSONArray arrayContents = new JSONArray();
		JSONObject datas = null;
		for (ContentTab contentTab : contents) {
			datas = new JSONObject();
			boolean result = populateContentData(datas, contentTab.getIntContentId());
			if (result)
				arrayContents.add(datas);
		}
		outDatas.put("content", arrayContents);
		jsonOut(outDatas.toString());
		return null;
	}
	
	/**
	 * 保存内容
	 * @return
	 * @author liuhj
	 * 2012-8-7下午03:21:22
	 * @throws IOException 
	 */
	@SuppressWarnings("unchecked")
	public String saveContent() throws IOException {
		json = URLDecoder.decode(json, "GBK");
		json = json.trim().substring(1, json.length() - 1).replaceAll("\\\\","");
		JSONObject datas = JSONObject.fromObject(json);
		log.info("保存分类的请求json数据:" + datas.toString());
		int cateId = datas.getInt("intCateId");
		ContentTab content = (ContentTab) JSONObject.toBean(datas
				.getJSONObject("contentInfo"), ContentTab.class);
		List<ContentImgTab> lstImg = JSONArray.toList(datas
				.getJSONArray("imgInfo"), ContentImgTab.class);
		List<AttributeDto> lstAttr = JSONArray.toList(datas
				.getJSONArray("attrInfo"), AttributeDto.class);
		appContentService.saveContent(getUserInfo(), cateId, content, lstImg,
				lstAttr, request.getContextPath());
		jsonOut(GenericConstants.OPERATION_SUCCESS);
		return null;
	}
	
	/**
	 * 根据内容ID查询内容
	 * @return
	 * @throws IOException
	 * @author liuhj
	 * 2012-8-7下午04:48:00
	 */
	public String viewContentById() throws IOException {
		int intContentId = JSONObject.fromObject(json).getInt("intContentId");
		JSONObject datas = new JSONObject();
		boolean success = populateContentData(datas, intContentId);
		if (!success) {
			jsonOut(GenericConstants.OPERATION_NULL);
			return null;
		}
		String result = "{\"content\":" + datas.toString() + "}";
		log.info("内容" + intContentId + "的json返回数据:" + result);
		jsonOut(result);
		return null;
	}

	/**
	 * 封装内容的基本信息，扩展属性信息，图片信息
	 * @param datas
	 * @param intContentId
	 * @return
	 * @throws IOException
	 * @author liuhj
	 * 2012-8-9下午05:58:50
	 */
	private boolean populateContentData(JSONObject datas,int intContentId) throws IOException {
		// 内容信息
		ContentTab content = contentService.get(intContentId );
		if (content == null) {
			return false;
		}
		ContentDto contentDto = new ContentDto();
		BeanUtils.copyProperties(content, contentDto);
		// 属性信息
		List<AttributeDto> lstAttr = appContentService.getAttribyteById(intContentId);
		// 图片信息
		List<ContentImgDtoForPru> lstImg = contentImgService.getImgsInfotById(intContentId);
		datas.put("contentInfo", contentDto);
		datas.put("attrInfo", lstAttr);
		datas.put("imgInfo", lstImg);
		return true;
	}
	
	/**
	 * 根据分类查询属性
	 * @return
	 * @author liuhj
	 * 2012-8-9上午09:50:31
	 * @throws IOException 
	 */
	public String getAllAttrByCateId() throws IOException {
		int intCateId = JSONObject.fromObject(json).getInt("intCateId");
		List<AttributeTab> attrs = catelogService
				.getAllAttributesByCateId(intCateId);
		if (attrs == null || attrs.size() <= 0) {
			jsonOut(GenericConstants.OPERATION_NULL);
			return null;
		}
		this.setLstAttributes(attrs);
		return SUCCESS;
	}
	
	/**
	 * 删除内容页面的视图
	 * @return
	 * @author liuhj
	 * 2012-8-10下午03:46:08
	 * @throws IOException 
	 */
	public String deleteViewInContent() throws IOException {
		int viewId = JSONObject.fromObject(json).getInt("intViewId");
		if (viewId != -1 ) {
			appPageService.deleteContentView(viewId);
		}
		jsonOut(GenericConstants.OPERATION_SUCCESS);
		return null;
	}
	
	/**
	 * 删除视图里的内容绑定
	 * @return
	 * @throws IOException
	 * @author liuhj
	 * 2012-8-10下午03:52:36
	 */
	public String deleteViewContentBind() throws IOException {
		int intViewContId = JSONObject.fromObject(json).getInt("intViewContId");
		if (intViewContId != -1) {
			appPageService.deleteContentBinding(intViewContId);
		}
		jsonOut(GenericConstants.OPERATION_SUCCESS);
		return null;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	public List<AttributeTab> getLstAttributes() {
		return lstAttributes;
	}

	public void setLstAttributes(List<AttributeTab> lstAttributes) {
		this.lstAttributes = lstAttributes;
	}
}
